package com.galaxis.wms.autoconfigure;

import com.galaxis.wms.security.DefaultSecurityContextRepository;
import com.galaxis.wms.security.authentication.DefaultRefreshJwtToken;
import com.galaxis.wms.security.authentication.DefaultVerifyJwtToken;
import com.galaxis.wms.security.handler.DefaultAuthenticationFailureHandler;
import com.galaxis.wms.security.handler.DefaultLoginFailureHandler;
import com.galaxis.wms.security.handler.DefaultLoginSuccessHandler;
import com.galaxis.wms.security.handler.DefaultLogoutSuccessHandler;
import com.galaxis.wms.security.login.*;
import com.yvan.groovy.autoconfigure.MultipleJdbcAutoConfigure;
import com.yvan.security.SecurityContextRepository;
import com.yvan.security.authentication.token.VerifyJwtToken;
import com.yvan.security.autoconfigure.SecurityAutoConfigure;
import com.yvan.security.config.SecurityConfig;
import com.yvan.security.crypto.PasswordEncoder;
import com.yvan.security.handler.AuthenticationFailureHandler;
import com.yvan.security.handler.LoginFailureHandler;
import com.yvan.security.handler.LoginSuccessHandler;
import com.yvan.security.handler.LogoutSuccessHandler;
import com.yvan.security.login.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * 作者：lizw <br/>
 * 创建时间：2021/12/30 20:52 <br/>
 */
@Configuration
@ConditionalOnProperty(name = SecurityConfig.PREFIX + ".enable", havingValue = "true", matchIfMissing = true)
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
@AutoConfigureAfter({MultipleJdbcAutoConfigure.class, SecurityAutoConfigure.class})
@EnableConfigurationProperties({SecurityConfig.class/*, WhIdConfig.class*/})
@Slf4j
public class WmsSecurityAutoConfigure {
    private final SecurityConfig securityConfig;
//    private final WhIdConfig whIdConfig;

    public WmsSecurityAutoConfigure(SecurityConfig securityConfig/*, WhIdConfig whIdConfig*/) {
        this.securityConfig = securityConfig;
//        this.whIdConfig = whIdConfig;
    }

    @Bean
    public DefaultRefreshJwtToken refreshJwtToken() {
        return new DefaultRefreshJwtToken(securityConfig);
    }

    @Bean
    public LoadUser loadUser() {
        return new DefaultLoadUser();
    }

    @Bean
    public LoginDataCollect loginDataCollect() {
        return new DefaultLoginDataCollect();
    }

    @Bean
    public VerifyLoginData verifyLoginData() {
        return new DefaultVerifyLoginData();
    }

    @Bean
    public VerifyUserInfo verifyUserInfo(PasswordEncoder passwordEncoder) {
        return new DefaultVerifyUserInfo(passwordEncoder);
    }

    @Bean
    public AddJwtTokenExtData addJwtTokenExtData() {
        return new DefaultAddJwtTokenExtData();
    }

    @Bean
    public VerifyJwtToken verifyJwtToken() {
        return new DefaultVerifyJwtToken();
    }

    @Bean
    public SecurityContextRepository securityContextRepository() {
        return new DefaultSecurityContextRepository();
    }

    @Bean
    public LoginSuccessHandler loginSuccessHandler() {
        return new DefaultLoginSuccessHandler(securityConfig/*, whIdConfig*/);
    }

    @Bean
    public LoginFailureHandler loginFailureHandler() {
        return new DefaultLoginFailureHandler();
    }

    @Bean
    public LogoutSuccessHandler logoutSuccessHandler() {
        return new DefaultLogoutSuccessHandler(securityConfig);
    }

    @Bean
    public AuthenticationFailureHandler authenticationFailureHandler() {
        return new DefaultAuthenticationFailureHandler(securityConfig);
    }
}
